package 动态网格;

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static int N = 110, n, m;
    static char[][] g = new char[N][N];
    static boolean[][] st = new boolean[N][N];
    static int[] dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        for (int cases = 1; cases <= T; cases++) {
            n = in.nextInt();
            m = in.nextInt();
            for (int i = 0; i < n; i++) {
                String s = in.next();
                for (int j = 0; j < m; j++) {
                    g[i][j] = s.charAt(j);
                }
            }
            System.out.printf("Case #%d:\n", cases);
            int q = in.nextInt();
            while (q-- > 0) {
                String[] s = in.next().split(" ");
                if (s[0].equals("Q")) {
                    System.out.println(work());
                } else {
                    int x = in.nextInt();
                    int y = in.nextInt();
                    int z = in.nextInt();
                    g[x][y] = (char) (z + '0');
                }
            }
        }
    }

    private static int work() {
        for (int i = 0; i < n; i++) {
            Arrays.fill(st[i], false);
        }
        int ret = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (!st[i][j] && g[i][j] == '1') {
                    dfs(i, j);
                    ret++;
                }
            }
        }
        return ret;
    }

    private static void dfs(int x, int y) {
        st[x][y] = true;
        for (int i = 0; i < 4; i++) {
            int a = dx[i] + x;
            int b = dy[i] + y;
            if (a >= 0 && a < n && b >= 0 && b < m && !st[a][b] && g[a][b] == '1') {
                dfs(a, b);
            }
        }
    }
}
